
import 'package:flutter/material.dart';
import 'package:flutter_hall/about.dart';
import 'package:flutter_hall/laboratory.dart';
import 'package:flutter_hall/map.dart';
import 'package:flutter_hall/map_list.dart';

import 'eventbus.dart';
import 'home_drawer.dart';

class NavigationHomeScreen extends StatefulWidget {
  const NavigationHomeScreen({Key? key}) : super(key: key);

  @override
  _NavigationHomeScreenState createState() => _NavigationHomeScreenState();
}

class _NavigationHomeScreenState extends State<NavigationHomeScreen> {
  DrawerIndex drawerIndex = DrawerIndex.home;

  @override
  Widget build(BuildContext context) {
    Widget body = const MainPage();
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
      ),
      drawer: HomeDrawer(
        screenIndex: drawerIndex,
        callBackIndex: changeIndex,
      ),
      onDrawerChanged: (isOpened) {
        isOpened = false;
      },
      body: body,
    );
  }

  void changeIndex(DrawerList d) {
    DrawerIndex drawerIndexdata = d.index;
    drawerIndex = drawerIndexdata;
    if (drawerIndex != DrawerIndex.home) {
      if (drawerIndex == DrawerIndex.supers) {
        Navigator.push(context,
                MaterialPageRoute(builder: (context) => const LaboratoryPage()))
            .then((value) => {
                  if (value != null)
                    {setBack(value, 1)}
                  else
                    {Navigator.pop(context)}
                });
      } else if (drawerIndex == DrawerIndex.about) {
        Navigator.push(context,
            MaterialPageRoute(builder: (context) => const AboutPage())).then((value) => Navigator.pop(context));
      } else {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => MapPage(
                      line: d.labelName,
                      lineIndex: d.arrayIndex,
                    ))).then((value) => {
              if (value != null) {setBack(value, 0)}
            });
      }
    }
  }

  void setBack(value, type) {
    Navigator.pop(context);
    bus.emit("forLineName", {'name': value, 'type': type});
  }
}
